package com.cmcc.passenger.ui.profile.viewmodel

import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.cmcc.passenger.data.AppRequest
import com.cmcc.passenger.data.entity.request.CodeRequest
import com.cmcc.passenger.data.entity.request.RegistRequest
import com.cmcc.passenger.data.entity.response.CarCategory
import com.cmcc.passenger.data.entity.response.DiverInfo
import com.cmcc.passenger.data.entity.response.PassengerInfo
import com.cmcc.shared.model.AppResponse
import com.cmcc.shared.model.MsgBean
import com.cmcc.shared.utils.L
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File
import java.util.concurrent.atomic.AtomicInteger
import kotlin.system.measureTimeMillis


class ProfileViewModel : ViewModel() {
    val passengerInfo = MutableLiveData<PassengerInfo>()
    val msgList = MutableLiveData<AppResponse<MsgBean>>()

    val info = MutableLiveData<AppResponse<DiverInfo>>()

    val upload = MutableLiveData<AppResponse<String>>()

    val upload2 = MutableLiveData<AppResponse<String>>()

    val regist = MutableLiveData<AppResponse<String>>()

    val code = MutableLiveData<AppResponse<String>>()

    val carCategory = MutableLiveData<List<CarCategory>>()

    var flag = AtomicInteger(2)

    val uploadCompleteMsg = MediatorLiveData<Boolean>()

    fun getPassengerInfo() {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = AppRequest.getInstance().getPassengerInfo()
                if (response.isSuccessful) {
                    val data = response.body()
                    passengerInfo.value = data?.data
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }


    /**
     * 获取消息列表
     */
    fun list(
        pageNum: Int,
        pageSize: Int
    ): MutableLiveData<AppResponse<MsgBean>> {
        viewModelScope.launch(Dispatchers.Main) {
            val response = AppRequest.getInstance().list(pageNum, pageSize)
            if (response.isSuccessful) {
                msgList.value = response.body()
            }


        }
        return msgList
    }


    /**
     * 获取司机信息
     */
    fun info(): MutableLiveData<AppResponse<DiverInfo>> {
        viewModelScope.launch(Dispatchers.Main) {
            val response = AppRequest.getInstance().info()
            if (response.isSuccessful) {
                info.value = response.body()
            }

        }
        return info
    }

    /**
     * 上传图片
     */
    fun uploadImg(
        fileType: Int,
        file: File,
        mediaType: String = "*/*"
    ): MutableLiveData<AppResponse<String>> {
        viewModelScope.launch(Dispatchers.Main)
        {
            val response = AppRequest.getInstance().upload(file, mediaType)
            if (response.isSuccessful) {
                when (fileType) {
                    1 -> {
                        upload.value = response.body()
                    }
                    2 -> {
                        upload2.value = response.body()
                    }
                }
            }
        }
        return upload
    }

    fun addAllUploadMsg() {
        uploadCompleteMsg.addSource(upload) {
            if (it.code == 200) {
                val minus = flag.decrementAndGet()
                if (minus == 0) {
                    uploadCompleteMsg.postValue(minus == 0)
                }
            }
        }
        uploadCompleteMsg.addSource(upload2) {
            if (it.code == 200) {
                val minus = flag.decrementAndGet()
                if (minus == 0) {
                    uploadCompleteMsg.postValue(minus == 0)
                }
            }
        }
    }


    /**
     * 注册
     */
    fun register(request: RegistRequest): MutableLiveData<AppResponse<String>> {
        viewModelScope.launch(Dispatchers.Main) {
            val response = AppRequest.getInstance().register(request)

            if (response.isSuccessful) {
                regist.value = response.body()
            }

        }
        return regist
    }

    /**
     * 获取验证码
     */
    fun code(request: CodeRequest): MutableLiveData<AppResponse<String>> {
        viewModelScope.launch(Dispatchers.Main) {
            val response = AppRequest.getInstance().code(request)

            if (response.isSuccessful) {
                code.value = response.body()
            }

        }
        return code
    }


    fun requestCarCategory() {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = AppRequest.getInstance().getCarCategory()
                if (response.isSuccessful) {
                    val body = response.body()
                    if (body?.data?.isNotEmpty() == true) {
                        carCategory.value = body.data.filterNot {
                            it.code.equals("vehicle_type_item_any")
                        }
                    }
                } else {
                    L.d("response failed")
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }


}